{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "GPU_id = 1\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = str(GPU_id)\n",
    "\n",
    "from torch_scatter import *\n",
    "from torch_geometric.utils import scatter_\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import numbers\n",
    "\n",
    "import torch\n",
    "from torch import _utils\n",
    "from fastai.torch_core import to_device\n",
    "import torch.nn.functional as F \n",
    "\n",
    "from fastai.basic_data import DataBunch\n",
    "from fastai.basic_data import *\n",
    "from fastai.tabular import *\n",
    "from fastai import *\n",
    "\n",
    "import copy\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\") \n",
    "\n",
    "from mpnn_model.common import *\n",
    "from mpnn_model.helpers import load_cfg\n",
    "from mpnn_model.model import *\n",
    "from mpnn_model.common_constants import * \n",
    "\n",
    "DATA_DIR = '/rapids/notebooks/srabhi/champs-2019/input'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_dict = { '1JHC': 'lmae', '2JHC': 'lmae', '3JHC': 'lmae', '3JHH': 'lmae',\n",
    "             '1JHN': 'mlmae' , '2JHN':'mlmae' , '3JHN':'mlmae', '2JHH':'mlmae'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to modify with the resulting model of self_training \n",
    "best_all_type_model_lmae = './models/mpnn_gauss_rank_LMAE_2CE_RNN_NODE_TYPE_ATOMIC_SEQ_BOOTSTRAP_V2_'\n",
    "best_all_type_model_mlmae = './models/mpnn_gauss_rank_MLMAE_2CE_RNN_NODE_TYPE_ATOMIC_SEQ_BOOTSTRAP_V2_'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Without GAUSSRANK : "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_model(fold, coupling_type, loss_criterion='lmae'): \n",
    "    cfg = load_cfg('./experiments/MPNN_RNN_MAE_WO_GAUSSRANK_SINGLE_TYPE.yaml')\n",
    "    if loss_criterion == 'lmae': \n",
    "        pretrained_dict =  torch.load(best_all_type_model_lmae+'_fold_%s_final_save.pth'%fold).state_dict()\n",
    "    else:\n",
    "        pretrained_dict =  torch.load(best_all_type_model_lmae+'_fold_%s_final_save.pth'%fold).state_dict()\n",
    "    ind = COUPLING_TYPE.index(coupling_type)\n",
    "    net= Net(cfg, y_range=[COUPLING_MIN_[ind], COUPLING_MAX_[ind]])\n",
    "    # 1. filter out unnecessary keys\n",
    "    pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict and not k.startswith('predict')}\n",
    "    # 2. overwrite entries in the existing state dict\n",
    "    model_dict.update(pretrained_dict) \n",
    "    # 3. load the new state dict\n",
    "    net.load_state_dict(pretrained_dict, strict=False)\n",
    "    # save model \n",
    "    torch.save(net, 'pre_trained_models/' + 'coupling_%s_%s_fold_%s_wo_gaussrank.pth'%(coupling_type, loss_criterion, fold))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "for fold in range(4):\n",
    "    for type_, criterion in model_dict.items():\n",
    "        save_model(fold, type_, criterion)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### With Gaussrank "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_model(fold, coupling_type, loss_criterion='lmae'): \n",
    "    cfg = load_cfg('/rapids/notebooks/srabhi/champs-2019/CherKeng_solution/fastai_code/experiments/MPNN_RNN_MAE_GAUSSRANK_SINGLE_TYPE.yaml')\n",
    "    if loss_criterion == 'lmae': \n",
    "        pretrained_dict =  torch.load('models/mpnn_gauss_rank_LMAE_2CE_RNN_NODE_TYPE_ATOMIC_SEQ_BOOTSTRAP_V2__fold_%s_final_save.pth'%fold).state_dict()\n",
    "    else:\n",
    "        pretrained_dict =  torch.load('models/mpnn_gauss_rank_MLMAE_2CE_RNN_NODE_TYPE_ATOMIC_SEQ_BOOTSTRAP_V2__fold_%s_final_save.pth'%fold).state_dict()\n",
    "    ind = COUPLING_TYPE.index(coupling_type)\n",
    "    net= Net(cfg, y_range=[-2.326753765513524, 2.3267537655135464])\n",
    "    # 1. filter out unnecessary keys\n",
    "    pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict and not k.startswith('predict')}\n",
    "    # 2. overwrite entries in the existing state dict\n",
    "    model_dict.update(pretrained_dict) \n",
    "    # 3. load the new state dict\n",
    "    net.load_state_dict(pretrained_dict, strict=False)\n",
    "    # save model \n",
    "    torch.save(net, 'pre_trained_models/' + 'coupling_%s_%s_fold_%s_gaussrank.pth'%(coupling_type, loss_criterion, fold))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "for fold in range(4):\n",
    "    for type_, criterion in model_dict.items():\n",
    "        save_model(fold, type_, criterion)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
